package com.hairline.service;

import com.hairline.bean.Cart;
import com.hairline.bean.CartExample;
import com.hairline.bean.bo.WxCartCheckedBo;
import com.hairline.bean.bo.WxCartUpdateBo;
import com.hairline.bean.vo.WxCartIndexVo;
import com.hairline.mapper.CartMapper;
import com.hairline.mapper.GoodsProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fenglin on 2020/9/5 5:14 AM
 */
@Service
public class WxCartServiceImpl implements WxCartService {

    @Autowired
    CartMapper cartMapper;
    @Autowired
    GoodsProductMapper goodsProductMapper;

    @Override
    public long cartGoodscount() {
        CartExample example = new CartExample();
        return cartMapper.countByExample(example);

    }


//    goodsCount: 6 --商品数量
//    checkedGoodsCount: 5 --已勾选的商品数量 checked=1 表示已勾选
//    goodsAmount: 835 -- 购物车中商品总价
//    checkedGoodsAmount: 236 --已勾选的商品总价
    /**
     * 购物车首页
     * @return
     */
    @Override
    public WxCartIndexVo cartIndex() {
        //获得商品数量
        CartExample goodsCountExample = new CartExample();
        CartExample.Criteria goodsCountCriteria = goodsCountExample.createCriteria();
        goodsCountCriteria.andDeletedEqualTo(false);
        Integer goodsCount = cartMapper.sumGoods(goodsCountExample);
        //long goodsCount = cartMapper.sumGoods(goodsCountExample);
        //已勾选的商品数量
        CartExample checkedGoodsCountExample = new CartExample();
        CartExample.Criteria checkedGoodsCountCriteria = checkedGoodsCountExample.createCriteria();
        checkedGoodsCountCriteria.andDeletedEqualTo(false);
        checkedGoodsCountCriteria.andCheckedEqualTo(true);
        //long checkedGoodsCount = cartMapper.sumGoods(checkedGoodsCountExample);
        Integer checkedGoodsCount = cartMapper.sumGoods(checkedGoodsCountExample);
        //购物车中商品总价
        CartExample goodsAmountExample = new CartExample();
        CartExample.Criteria goodsAmountCriteria = goodsAmountExample.createCriteria();
        goodsAmountCriteria.andDeletedEqualTo(false);
        Integer goodsAmount = cartMapper.countGoodsPrice(goodsAmountExample);
        //已勾选的商品总价
        CartExample checkedGoodsAmountExample = new CartExample();
        CartExample.Criteria checkedGoodsAmountCriteria = checkedGoodsAmountExample.createCriteria();
        checkedGoodsAmountCriteria.andDeletedEqualTo(false);
        checkedGoodsAmountCriteria.andCheckedEqualTo(true);
        Integer checkedGoodsAmount = cartMapper.countGoodsPriceChecked(checkedGoodsAmountExample);

        Map<String, Object> map = new HashMap<>();
        map.put("goodsCount", goodsCount);
        map.put("checkedGoodsCount", checkedGoodsCount);
        map.put("goodsAmount", goodsAmount);
        map.put("checkedGoodsAmount", checkedGoodsAmount);

        //获取cart表中所有数据
        CartExample cartExample = new CartExample();
        CartExample.Criteria criteria = cartExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<Cart> cartList = cartMapper.selectByExample(cartExample);

        //返回
        WxCartIndexVo wxCartIndexVo = new WxCartIndexVo(map, cartList);
        return wxCartIndexVo;
    }

    @Override
    public int cartUpdate(WxCartUpdateBo wxCartUpdateBo) {
        //wxCartUpdateBo:  number--当前商品在购物车中的数量
        //根据goodsId查询库存numberInRepertory 加购数量大于库存量，则不会添加成功
        int goodsId = wxCartUpdateBo.getGoodsId();
        int numberInRepertory = goodsProductMapper.selectNumberByGoodsId(goodsId);
        if(wxCartUpdateBo.getNumber() > numberInRepertory ){
            return 710;
        }
        //更新cart表
        CartExample cartExample = new CartExample();
        CartExample.Criteria criteria = cartExample.createCriteria();
        criteria.andIdEqualTo(wxCartUpdateBo.getId());
        Cart cart = new Cart();
        cart.setNumber(wxCartUpdateBo.getNumber());
        cartMapper.updateByExampleSelective(cart, cartExample);
        return 200;
    }

    @Override
    public WxCartIndexVo cartChecked(WxCartCheckedBo wxCartCheckedBo) {
        //接收 {"productIds":[12,54],"isChecked":0}
        //遍历productIds数组，根据productId更新cart表中isChecked的值
        Integer[] productIds = wxCartCheckedBo.getProductIds();
        //boolean checked = wxCartCheckedBo.isChecked();
        int checked = wxCartCheckedBo.getIsChecked();
//        boolean checkBoolean = false;
//        if(wxCartCheckedBo.getIsChecked() == 1){
//            checkBoolean = true;
//        }

        for(Integer i : productIds){
            CartExample example = new CartExample();
            CartExample.Criteria criteria = example.createCriteria();
            criteria.andProductIdEqualTo(i);

            Cart cart = new Cart();
            //cart.setChecked(wxCartCheckedBo.isChecked());
            int checked2 = wxCartCheckedBo.getIsChecked();
            //cart.setChecked();
            cart.setChecked(checked2);

            //TODO
            //criteria.andCheckedEqualTo(cart.getChecked());
            //cartMapper.updateByExample(cart, example);
            int i1 = cartMapper.updateByExampleSelective(cart, example);
            System.out.println("i1 " + i1);
        }
        //获得当前购物车页面
        WxCartIndexVo wxCartIndexVo = cartIndex();
        return wxCartIndexVo;
    }
}
